﻿using System;
using System.Xml;
using System.Windows.Forms;
using SdmxMl.Common;


namespace SdmxMl.Structure
{
    public class MetadataflowType : Artefact, IFlowRef
    {
        #region Properties

        public override ArtefactRefTypedList ArtefactRefList
        {
            get
            {
                ArtefactRefTypedList list = new ArtefactRefTypedList();
                if (MetadataStructureRef.IsFilled)
                {
                    ArtefactRefTyped at = new ArtefactRefTyped(
                       SdmxMl.Manager.SdmxArtefactType.MetadataStructureDefinitions,
                       MetadataStructureRef.Id, MetadataStructureRef.AgencyId, MetadataStructureRef.Version);
                    if (list.Contains(at.Type, at.FullIdent) == false)
                        list.Add(at);
                }

                return list;
            }
        }


        private MetadataFlowInsider _metadataFlowInsider;
        public MetadataStructureRefType MetadataStructureRef
        {
            get { return _metadataFlowInsider.MetadataStructureRef; }
        }

        #endregion //Properties

        #region Constructors

        public MetadataflowType()
            : base()
        {
            _metadataFlowInsider = new MetadataFlowInsider();
        }


        public MetadataflowType(XmlNode nodeArtefact, SdmxNamespaceManager nsmgr)
            : base(nodeArtefact, nsmgr)
        {
            _metadataFlowInsider = new MetadataFlowInsider(nodeArtefact, nsmgr);
        }

        #endregion //Constructors

        #region Tree

        public override TreeNode FillNode(TreeNode parentNode)
        {
            TreeNode t = base.FillNode(parentNode);
            TreeNode td = new TreeNode(_metadataFlowInsider.ToString(), 0, 0);
            t.Nodes.Add(td);
            td.Tag = _metadataFlowInsider;

            return t;
        }

        #endregion // Tree

        #region XML serialize

        public override void WriteXmlInsider(XmlTextWriter xw)
        {
            _metadataFlowInsider.WriteXmlInsider(xw);
        }

        #endregion

        #region IFlowRef Members

        public ArtefactRefTyped ReferencedArtefact
        {
            get
            {
                return new ArtefactRefTyped(SdmxMl.Manager.SdmxArtefactType.MetadataStructureDefinitions,
                    MetadataStructureRef.FullIdent);
            }
        }

        #endregion

        #region Artefact Type

        public override Manager.SdmxArtefactType ArType
        {
            get { return Manager.SdmxArtefactType.Metadataflows; }
        }

        #endregion
    }

    /// <summary>
    ///  Intermediate Class constructed to help Edition
    /// </summary>
    public class MetadataFlowInsider
    {
        #region Properties

        private MetadataStructureRefType _metadataStructureRef;
        public MetadataStructureRefType MetadataStructureRef
        {
            get { return _metadataStructureRef; }
        }


        #endregion //Properties

        #region Constructors

        public MetadataFlowInsider()
        {
            Init();
        }

        private void Init()
        {
            _metadataStructureRef = new MetadataStructureRefType();
        }

        public MetadataFlowInsider(XmlNode nodeArtefact, SdmxNamespaceManager nsmgr)
        {
            Init();

            if (nsmgr.VersionSdmx == SdmxVersion.SDMX_21)
            {
                _metadataStructureRef = new MetadataStructureRefType();
                string query = "./structure:Structure/Ref";
                XmlNode nd = nodeArtefact.SelectSingleNode(query, nsmgr);
                if (nd != null)
                {
                    _metadataStructureRef.Id = SdmxMl.Helper.Xmlhelper.GetAtt(nd, "id");
                    _metadataStructureRef.AgencyId = SdmxMl.Helper.Xmlhelper.GetAtt(nd, "agencyID");
                    _metadataStructureRef.Version = SdmxMl.Helper.Xmlhelper.GetAtt(nd, "version");
                }
            }
            else
            {
                // MSD Ref ?
                XmlNode nodeMsd = nodeArtefact.SelectSingleNode("./structure:MetadataStructureRef", nsmgr);
                if (nodeMsd != null)
                    _metadataStructureRef = new MetadataStructureRefType(nodeMsd, nsmgr);
            }


        }

        #endregion //Constructors

        #region XML serialize

        public void WriteXmlInsider(XmlTextWriter xw)
        {
            string prefix = "structure";
            if (SdmxNamespaceManager.SdmxTarget == SdmxVersion.SDMX_21)
            {
                xw.WriteStartElement(prefix, "Structure", null);
                xw.WriteStartElement("Ref");
                xw.WriteAttributeString("id", _metadataStructureRef.Id);
                xw.WriteAttributeString("version", _metadataStructureRef.Version);
                xw.WriteAttributeString("agencyID", _metadataStructureRef.AgencyId);
                xw.WriteEndElement();
                xw.WriteEndElement();
            }
            else
            {
                _metadataStructureRef.WriteXml(xw, prefix, "MetadataStructureRef");
            }
        }

        #endregion

        #region String Representation

        public override string ToString()
        {
            return _metadataStructureRef.ToString();
        }
        #endregion
    }
}

